package com.billard.controller;


import com.billard.dto.TableStatusDTO;
import com.billard.entity.Table;
import com.billard.result.Result;
import com.billard.service.TableService;
import com.billard.vo.TableVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 球台控制器
 */
@RestController
@RequestMapping("/table")
@Tag(name = "球台管理", description = "球台相关接口")
public class TableController {

    @Autowired
    private TableService tableService;

    @GetMapping("/list")
    @Operation(summary = "查询所有球台", description = "获取所有球台信息")
    @PreAuthorize("@securityPermission.hasPermission('table:list')")
    public Result<List<TableVO>> getAllTables() {
        List<TableVO> tables = tableService.getAllTables();
        return Result.success(tables);
    }

    @GetMapping("/status/{status}")
    @Operation(summary = "根据状态查询球台", description = "根据状态查询球台信息")
    @PreAuthorize("@securityPermission.hasPermission('table:list')")
    public Result<List<Table>> getTablesByStatus(@PathVariable Integer status) {
        List<Table> tables = tableService.getTablesByStatus(status);
        return Result.success(tables);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询球台详情", description = "根据ID查询球台详细信息")
    @PreAuthorize("@securityPermission.hasPermission('table:view')")
    public Result<Table> getTableById(@PathVariable Long id) {
        Table table = tableService.getTableById(id);
        return Result.success(table);
    }

    @PostMapping("/status/change")
    @Operation(summary = "变更球台状态", description = "变更球台状态（开始使用、结束使用、预约、维护等）")
    @PreAuthorize("@securityPermission.hasPermission('table:status:change')")
    public Result<Void> changeTableStatus(@Valid @RequestBody TableStatusDTO tableStatusDTO) {
        tableService.changeTableStatus(tableStatusDTO);
        return Result.success();
    }

    @PostMapping("/{id}/start")
    @Operation(summary = "开始使用球台", description = "开始使用指定球台")
    @PreAuthorize("@securityPermission.hasPermission('table:start')")
    public Result<Void> startUsingTable(@PathVariable Long id,
                                           @RequestParam String customerName,
                                           @RequestParam String customerPhone) {
        tableService.startUsingTable(id, customerName, customerPhone);
        return Result.success();
    }

    @PostMapping("/{id}/end")
    @Operation(summary = "结束使用球台", description = "结束使用指定球台并结算费用")
    @PreAuthorize("@securityPermission.hasPermission('table:end')")
    public Result<Void> endUsingTable(@PathVariable Long id) {
        tableService.endUsingTable(id);
        return Result.success();
    }

    @PostMapping("/{id}/pause")
    @Operation(summary = "暂停球台", description = "暂停指定球台使用")
    @PreAuthorize("@securityPermission.hasPermission('table:pause')")
    public Result<Void> pauseTable(@PathVariable Long id) {
        tableService.pauseTable(id);
        return Result.success();
    }

    @PostMapping("/{id}/resume")
    @Operation(summary = "恢复球台", description = "恢复指定球台使用")
    @PreAuthorize("@securityPermission.hasPermission('table:resume')")
    public Result<Void> resumeTable(@PathVariable Long id) {
        tableService.resumeTable(id);
        return Result.success();
    }

    @PostMapping
    @Operation(summary = "创建球台", description = "创建新的球台")
    @PreAuthorize("@securityPermission.hasPermission('table:add')")
    public Result<Void> createTable(@Valid @RequestBody Table table) {
        tableService.createTable(table);
        return Result.success();
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新球台", description = "更新球台信息")
    @PreAuthorize("@securityPermission.hasPermission('table:update')")
    public Result<Void> updateTable(@PathVariable Long id, @Valid @RequestBody Table table) {
        table.setId(id);
        tableService.updateTable(table);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除球台", description = "删除指定球台")
    @PreAuthorize("@securityPermission.hasPermission('table:delete')")
    public Result<Void> deleteTable(@PathVariable Long id) {
        tableService.deleteTable(id);
        return Result.success();
    }
}
